package 哈希表.重写hash和equals;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/3/8 14:06
 * @description:
 * @version:
 */

/**
 * 自己的理解：之所以重写equals方法后还要重写hashcode是因为在中采用的是链地址法来解决冲突，也就是说会首先根据对象的hashcode来进行取模运算
 * 找到对应的线性表下标索引。然后再进行equals方法的比较。如果不重写hashcode方法，那么虽然两个对象的值相同，但是根据默认方法生成的hashcode值不同，会被分配到不同索引处，所以肯定是不相等的。
 */
public class Employee
{
    private Integer id;
    private String firstname;
    private String lastName;
    private String department;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }

    @Override
    public boolean equals(Object o) {
        if(o == null)
        {
            return false;
        }
        if (o == this)
        {
            return true;
        }
        if (getClass() != o.getClass())
        {
            return false;
        }

        Employee e = (Employee) o;
        return (this.getId() == e.getId());
    }

    @Override
    public int hashCode()
    {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + getId();
        return result;
    }


}

